package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.display.newAttack.vo.PetVo;
	import engine.flag.ResourcesPath;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.MouseEvent;
	
	import newx.com.control.comboBox.NxComboBoxEvent;
	import newx.com.control.window.NxWindowEvent;
	import newx.com.config.StaticIni;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	/**
	 *宠物传授  （已废弃） 
	 * @author liuel
	 * 
	 */
	public class PetImpartLogic
	{
		private static var _self:PetImpartLogic;
		private var _ui	:PetImpartUI;
		private var _model:ModelLocator;
		
		private var _bindMang:BindManage;
		private var _levelIndex:int =-1;
		private var _petArr:Array=[];
		
		//被传授的
		private var _setdate:Object;
		//传授的对象
		private var _impartObj:*
		public function PetImpartLogic()
		{
		}
		public static function getInstance():PetImpartLogic
		{
			if(_self == null)
			{
				_self=new PetImpartLogic();
			}
			return _self;
		}
		public function  Init():void
		{
			_bindMang =BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_ui = PetImpartUI.getInstance();
		_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_PET_COPY_RES_LEVEL),getLevel);
		
		}
		public function	BeforeShow():void
		{
			addEvent();
			_ui.infolabel.htmlText=StaticIni.getSwfLangStr("Pet_Impart_Des");
		}
		public function	AfterHide():void
		{
			_ui.comboBox.clear();
			_ui.rightLevel.htmlText ="";
			_setdate=null;
			removeEvent();
		}
		public  function set impartObj(date:*):void
		{
			_impartObj=date;
			updateImpart();
			getInfo();
			if(_ui.created==false)
			{
				_ui.showOrHide();
			}
		}
		
		private function bind():void
		{
//			if(_levelIndex ==-1)
//			{
//				_levelIndex =_bindMang.BindProp(_setdate as ObjectData,"Level",this,"level");
//			}

		}
		private function unbind():void
		{
//			if(_levelIndex !=-1)
//			{
//				_levelIndex =_bindMang.UnbindProp(_setdate as ObjectData,"Level",_levelIndex);
//			}
		}
		public function set level(value:int):void
		{
			_ui.rightLevel.htmlText=value.toString();
		}
		/**
		 *更新传授对象 
		 * 
		 */		
		public function updateImpart():void
		{
			_ui.picMC1.source = ResourcesPath.getHeadPic(_impartObj.GetProperties("Model")) ;
			_ui.leftName.htmlText =_impartObj.GetProperties("PetName");
			_ui.leftLevel.htmlText=_impartObj.GetProperties("Level")
		}
		private function addEvent():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btn.addEventListener(MouseEvent.CLICK,onClick);
			_ui.comboBox.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK,onClickBox);
		}
		private function removeEvent():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btn.removeEventListener(MouseEvent.CLICK,onClick);
			_ui.comboBox.removeEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK,onClickBox);
		}
		private function onClose(e:NxWindowEvent):void
		{
			_ui.hide();
		}
		private function updateBeimpart(date:*):void
		{
			_ui.picMC2.source =ResourcesPath.getHeadPic(date.model);
			_ui.rightLevel.htmlText =date.level;
		}
		
		private function complete():void
		{
			_ui.expLabel.visible=false;
		}
		private function send():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUET_COPY_PET_RES_LEVEL,_impartObj.GetProperties("ConfigID"),int(_setdate.configid));
		}
		private function getLevel(e:CustomMsgEvent):void
		{
			_ui.rightLevel.htmlText =e.pList[1];
		}
		/**
		 *更新容器信息 
		 * @param e
		 * 
		 */		
		public function getInfo():void
		{
			_petArr=[];
			var _petVec:Vector.<PetVo>=new Vector.<PetVo>();;
			_petVec =PetLoic.getInstance().petArr;
			for(var i:String in _petVec )
			{
				if(_petVec[int(i)].configid ==_impartObj.GetProperties("ConfigID") ||_petVec[int(i)].beimpart == 1)
						continue;
				_petArr.push({label:_petVec[int(i)].name,date:_petVec[int(i)]})
			}
			if(_petArr.length>0)
			{
				_ui.comboBox.dataProvider=_petArr;
				updateBeimpart(_petArr[0].date)
				_setdate =_petArr[0].date;
				send();
			}
			else
			{
				_ui.comboBox.dataProvider=[];
				_ui.comboBox.clear();
				_ui.rightLevel.htmlText ="";
				_setdate=null;
			}
			if(_setdate !=null)
			{
				unbind();
				bind()
			}
			
		}
		private function onClickBox(e:NxComboBoxEvent):void
		{
			updateBeimpart(e.item.date);
			_setdate=e.item.date;
			send();
		}
		/**
		 * 请求传授 
		 * @param e
		 * 
		 */		
		private function onClick(e:MouseEvent):void
		{
			if(_setdate !=null)
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_PET_COPY_EXP,_impartObj.GetProperties("ConfigID"),int(_setdate.configid));
			}
			_ui.hide();
		}
		
	}
}